var express = require('express');
var mysql = require('mysql');
let jwt = require('jsonwebtoken');

var search = function (ID, password, callback){
    //建立连接
    var connection = mysql.createConnection({
        host     : '148.70.88.72',
        user     : 'test',
        password : '5280d5e6515378d4',
        database : 'test'
    })
    //启动连接
    connection.connect(function (err){
        if(err)
            console.log('数据库连接失败');
    });
    var query = "select * from `user` where ID=? and password=?";
    //console.log(query);
    connection.query(query,[ID,password], function (err, result) {
        if (err) throw err;
        callback(result);
        connection.end();
    })
}
var searchMaxUserID = function (callback){
    //建立连接
    var connection = mysql.createConnection({
        host     : '148.70.88.72',
        user     : 'test',
        password : '5280d5e6515378d4',
        database : 'test'
    })
    //启动连接
    connection.connect(function (err){
        if(err)
            console.log('数据库连接失败');
    });
    var query = "select max(userID) as ui from user";
    //console.log(query);
    connection.query(query, function (err, result) {
        if (err) throw err;
        callback(result[0].ui);
        connection.end();
    })
}
var searchID = function (ID, callback){
    //建立连接
    var connection = mysql.createConnection({
        host     : '148.70.88.72',
        user     : 'test',
        password : '5280d5e6515378d4',
        database : 'test'
    })
    //启动连接
    connection.connect(function (err){
        if(err)
            console.log('数据库连接失败');
    });
    var query = "SELECT * FROM `user` WHERE ID=?"
    //console.log(query);
    connection.query(query,[ID], function (err, result) {
        if (err) throw err;
        //打印查询结果
        //console.log("查询结果为：", result);
        callback(result);
        connection.end();
    })
}
var add = function (userID,ID, password){
    //建立连接
    var connection = mysql.createConnection({
        host     : '148.70.88.72',
        user     : 'test',
        password : '5280d5e6515378d4',
        database : 'test'
    })
    //启动连接
    connection.connect(function (err){
        if(err)
            console.log('数据库连接失败');
    });
    //console.log('password = '+password);
    var query = 'insert into `user`(userID,ID,password) values (?,?,?)';
    connection.query(query,[userID,ID,password], function (err, result) {
        if (err) throw err;
        connection.end();
    })

}
var del = function (ID) {
    //建立连接
    var connection = mysql.createConnection({
        host: '148.70.88.72',
        user: 'test',
        password: '5280d5e6515378d4',
        database: 'test'
    })
    //启动连接
    connection.connect(function (err) {
        if (err)
            console.log('数据库连接失败');
    });
    var query = "DELETE FROM `user` where id=?";

    //console.log(query);
    connection.query(query,[ID], function (err, result) {
        if (err) throw err;
        connection.end();
    })
}

var update = function (ID,password){
    //建立连接
    var connection = mysql.createConnection({
        host: '148.70.88.72',
        user: 'test',
        password: '5280d5e6515378d4',
        database: 'test'
    })
    //启动连接
    connection.connect(function (err) {
        if (err)
            console.log('数据库连接失败');
    });
    var query = "update user set password=? where id=?";
    //console.log(query);
    connection.query(query,[password,ID], function (err, result) {
        if (err) throw err;
        connection.end();
    })
}

var createToken = function (userID){
    let payload = {userID:userID};
    let secret = 'shhhhh';
    return jwt.sign(payload, secret);
}
var checkToken = function (userID,token){
     return jwt.verify(token, 'shhhhh', function(err, decoded) {
        if (err) {
           return false;
        }
        //console.log('decoded='+decoded.userID);
        if(decoded.userID == userID)
            return true;
        else
            return false;
    });
}


exports.search = search;
exports.searchID = searchID;
exports.searchMaxUserID = searchMaxUserID;
exports.add = add;
exports.del = del;
exports.update = update;
exports.createToken = createToken;
exports.checkToken = checkToken;
